﻿using ALEXFW.Authorization.Users;
using ALEXFW.BasicModel;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;

namespace ALEXFW.EntityFrameworkCore.Seed.EntitySeed
{
    public class DefaultSeed
    {
        private ALEXFWDbContext _context;
        private List<Guid> SchoolIds { get; set; }
        private List<Guid> CollegeIds { get; set; }
        private List<Guid> ClassesIds { get; set; }
        private List<Category> Categories { get; set; }

        public DefaultSeed(ALEXFWDbContext context)
        {
            _context = context;
            SchoolIds = new List<Guid>();
            CollegeIds = new List<Guid>();
            ClassesIds = new List<Guid>();
            Categories = new List<Category>();
        }

        public void Create()
        {
            CreateSchool();
            CreateCollege();
            CreateClasses();
            CreateStudent();
            _context.SaveChanges();

            CreateTeacher();
            CreateCategory();
            CreateGroup();
            CreateExercise();
            _context.SaveChanges();

            ChangeStudentInfo();
        }

        private void CreateSchool()
        {
            var school = _context.Schools.IgnoreQueryFilters().FirstOrDefault(x => x.Name.Contains("柳州职业技术学院"));
            if (school == null)
            {
                var school1 = new School
                {
                    Id = Guid.NewGuid(),
                    Name = "柳州职业技术学院社湾校区"
                };
                var school2 = new School
                {
                    Id = Guid.NewGuid(),
                    Name = "柳州职业技术学院官塘校区"
                };
                SchoolIds.Add(_context.Schools.Add(school1).Entity.Id);
                SchoolIds.Add(_context.Schools.Add(school2).Entity.Id);
            }
        }

        private void CreateCollege()
        {
            var college = _context.Colleges.IgnoreQueryFilters().FirstOrDefault(x => x.Name.Contains("学院"));
            if (college == null)
            {
                var college1 = new College
                {
                    Id = Guid.NewGuid(),
                    Name = "信息工程学院",
                    SchoolID = SchoolIds[0]
                };
                var college2 = new College
                {
                    Id = Guid.NewGuid(),
                    Name = "艺术学院",
                    SchoolID = SchoolIds[0]
                };
                var college3 = new College
                {
                    Id = Guid.NewGuid(),
                    Name = "财经学院",
                    SchoolID = SchoolIds[1]
                };
                var college4 = new College
                {
                    Id = Guid.NewGuid(),
                    Name = "机电学院",
                    SchoolID = SchoolIds[1]
                };
                CollegeIds.Add(_context.Colleges.Add(college1).Entity.Id);
                CollegeIds.Add(_context.Colleges.Add(college2).Entity.Id);
                CollegeIds.Add(_context.Colleges.Add(college3).Entity.Id);
                CollegeIds.Add(_context.Colleges.Add(college4).Entity.Id);
            }
        }

        private void CreateClasses()
        {
            if (CollegeIds == null && CollegeIds.Count == 0) return;

            for (int i = 0; i < CollegeIds.Count; i++)
            {
                for (int j = 1; j <= 2; j++)
                {
                    var classes = new Classes
                    {
                        Id = Guid.NewGuid(),
                        Name = j + "班",
                        Year = "2020",
                        CollegeID = CollegeIds[i]
                    };
                    ClassesIds.Add(_context.Classes.Add(classes).Entity.Id);
                }
            }
        }

        private void CreateStudent()
        {
            if (ClassesIds == null && ClassesIds.Count == 0) return;

            for (int i = 0; i < ClassesIds.Count; i++)
            {
                var student = new Student
                {
                    Id = Guid.NewGuid(),
                    Name = "张三",
                    Sex = true,
                    StudentID = "20200001",
                    ClassesID = ClassesIds[i]
                };
                _context.Students.Add(student);
            }
        }

        private void CreateTeacher()
        {
            var teacher = _context.Teachers.IgnoreQueryFilters().FirstOrDefault(x=>x.Name=="力士");
            if (teacher == null)
            {
                teacher = new Teacher
                {
                    Id = Guid.NewGuid(),
                    Name = "力士",
                    Sex = true,
                    PhoneNumber = "13455555555",
                    Email = "123@123.com",
                    CollegeID = _context.Colleges.FirstOrDefault().Id,
                    SchoolID = _context.Schools.FirstOrDefault().Id,
                    UserID = _context.Users.IgnoreQueryFilters().FirstOrDefault(x => x.UserName == "aa").Id
                };
                _context.Teachers.Add(teacher);
                _context.SaveChanges();
            }
        }


        private void ChangeStudentInfo()
        {
            foreach (var schoolID in SchoolIds)
            {
                var colleges = _context.Colleges.Where(x => x.SchoolID == schoolID).ToList();
                foreach (var college in colleges)
                {
                    var classess = _context.Classes.Where(x => x.CollegeID == college.Id).ToList();
                    foreach (var classes in classess)
                    {
                        var student = _context.Students.Where(x => x.ClassesID == classes.Id).FirstOrDefault();
                        student.CollegeID = college.Id;
                        student.SchoolID = schoolID;
                        _context.Students.Update(student);
                        _context.SaveChanges();
                    }
                }
            }
            _context.SaveChanges();
        }

        private void CreateCategory()
        {
            var category = _context.Categories.IgnoreQueryFilters().FirstOrDefault(x => x.Name == "前端");
            if (category == null)
            {
                var category1 = new Category
                {
                    Id = Guid.NewGuid(),
                    Name = "前端",
                    Description = "前端开发，使用用Vue开发"
                };
                var category2 = new Category
                {
                    Id = Guid.NewGuid(),
                    Name = ".Net开发",
                    Description = "后端开发，使用C#开发"
                };
                _context.Categories.Add(category1);
                _context.Categories.Add(category2);
                Categories.Add(category1);
                Categories.Add(category2);
            }
        }

        private void CreateGroup()
        {
            if (Categories != null)
            {
                foreach (var item in Categories)
                {
                    for (int i = 0; i < 2; i++)
                    {
                        var group = new Group
                        {
                            Id = Guid.NewGuid(),
                            CategoryID = item.Id,
                            Name = item.Name + i + "组",
                            TeacherID =_context.Teachers.FirstOrDefault().Id
                        };
                        _context.Groups.Add(group);
                    }
                }
            }
            _context.SaveChanges();
        }

        private void CreateExercise()
        {
            var exerci = _context.Exercises.IgnoreQueryFilters().FirstOrDefault(x => x.Name == "测试1");
            if (exerci == null)
            {
                var group = _context.Groups.FirstOrDefault();
                var exercise = new Exercise
                {
                    Id = Guid.NewGuid(),
                    GroupID = group.Id,
                    Name = "测试1",
                    Issuser = group.TeacherID
                };
                _context.Exercises.Add(exercise);
            }
        }
    }
}
